local PartnerSkill = class("PartnerSkill")
local Util = require("app.utils.Util")
function PartnerSkill:ctor()
    self._isUnlock = false
    self._lv = 1
    self._disableUpgrade = false
end


-- function PartnerSkill:initByPartnerIdAndSlotAndLevel(partnerID, slot, lv)
--     self._partnerId = partnerID
--     self._slot = slot
--     self._lv = lv
--     self._conf = GD:queryPartnerSkillsByPartnerIdAndLv(partnerID, slot, lv)
--     assert(self._conf ~= nil, string.format("initByPartnerIdAndSlotAndLevel conf is nil partnerID = %s  slot = %s lv = %s ", partnerID, slot, lv))
--     self._nextSkillId = tonumber(self._conf.nextSkill)
-- end


function PartnerSkill:initByConf(conf)
    assert(conf ~= nil, "PartnerSkill initByConf conf is nil")
    self._id = tonumber(conf.ID)
    self._partnerId = tonumber(conf.belongTo)
    self._slot = tonumber(conf.sort)
    self._lv = tonumber(conf.lv)
    self._conf = conf
    self._nextSkillId = tonumber(self._conf.nextSkill)
    self._skillConf = GD:getClassSkill(self._id)
    assert(self._skillConf ~= nil, "PartnerSkill skill id conf id = "..self._id)
end


function PartnerSkill:setLevel(lv)
    if self._lv ~= lv then
        self._lv = lv
        self._conf = GD:queryPartnerSkillsByPartnerIdAndLv(self._partnerId, self._slot, lv)
        assert(self._conf ~= nil, string.format("setLevel conf is nil partnerID = %s  slot = %s lv = %s ", self._partnerId, self._slot, lv))
        self._nextSkillId = tonumber(self._conf.nextSkill)
    end
end

function PartnerSkill:setSkillId(id)
    if self._id ~= id then
        self._id = id
        self._conf = GD:queryPartnerBySkillById(id)
        assert(self._conf ~= nil, string.format("setSkillId conf is nil id = %s ", id))
        self._lv = tonumber(self._conf.lv)
        self._nextSkillId = tonumber(self._conf.nextSkill)
        self._skillConf = GD:getClassSkill(self._id)
        assert(self._skillConf ~= nil, "PartnerSkill skill id conf id = "..self._id)
    end
end


function PartnerSkill:isMaxLevel()
    return self._nextSkillId == -1
end

function PartnerSkill:getLevel()
    return self._lv
end


function PartnerSkill:setUnlock(trueOrFalse)
    self._isUnlock = trueOrFalse
end

function PartnerSkill:isUnlock()
    return self._isUnlock
end

function PartnerSkill:getName()
    return L(self._skillConf.name)
end

-- 获取 class_skill表中id
function PartnerSkill:getClassSkillId()
    return tonumber(self._skillConf.ID)
end

-- 获取当前格子
function PartnerSkill:getSlot()
    return tonumber(self._conf.sort)
end

function PartnerSkill:getIcon()
    return Res.skillIconPath..self._skillConf.icon..".png"
end

function PartnerSkill:getDesc()
    return L(self._skillConf.des)
end

function PartnerSkill:getNextLevelDesc()
    return L(self._skillConf.nextLvDes)
    -- if self:isMaxLevel() then
    --     return ""
    -- end
    -- local skillConf = GD:getClassSkill(self._nextSkillId)
    -- if skillConf then
    --     return L(self._skillConf.des)
    -- end
    -- return ""
end

function PartnerSkill:getSkillCd()
    return self:_getSkillCd(self._id)
end


function PartnerSkill:getNextSkillCd()
    if self:isMaxLevel() then
        return self:getSkillCd()
    end
    return self:_getSkillCd(self._nextSkillId)
end

function PartnerSkill:_getSkillCd(skillId)
    local battleSkillId = nil
    local skillConf = GD:getClassSkill(skillId)
    if skillConf then
        local skillPars = string.split( skillConf.skillPar , ";" )
        for _,par in pairs( skillPars ) do
            local skillParArray = string.split(par,"|")
            if skillParArray[1]=="1" then
                battleSkillId = tonumber(skillParArray[2])
                break
            end
        end
        
        local _, _ , _, cd = UD:getSkillCost(battleSkillId, skillConf.skillLv)
        return cd
    end
    return 0
end

function PartnerSkill:getUpgradeCost()
    if self:isMaxLevel() then
        return {}
    end 
    local costs = string.split(self._conf.upgradeCost, ";")
    local items = {}
    for k, v in pairs(costs) do
        local itemInfo = string.split(v, "|")
        table.insert(items, {
            type = tonumber(itemInfo[1]),
            id = tonumber(itemInfo[2]),
            num = tonumber(itemInfo[3])
        })
    end
    return items
end


function PartnerSkill:getId()
    return self._id
end

-- 检查是否可以升级
function PartnerSkill:checkCanUpgrade()
    if not self:isUnlock() then
        return false, 1
    end
    if self:isMaxLevel() then
        return false, 2
    end 
    local itemDatas = self:getUpgradeCost()
    local ret = true
    for k ,v in pairs(itemDatas) do
        if v.id ~= 0 then
            local num = UD:getItemAmountByTid(v.id)
            if v.num > num then
                ret = false
                break
            end
        else
            local num =UD:getDaibiValue(v.type)
            if v.num > num then
                ret = false
                break
            end
        end
    end
    return ret, 3
end

function PartnerSkill:setDisableUpgrade(trueOrFalse)
    self._disableUpgrade = trueOrFalse
end

function PartnerSkill:isDisableUpgrade()
    return self._disableUpgrade
end

-- debug
function PartnerSkill:getDumpTable()
    local t = {
        _id = self._id,
        _partnerId = self._partnerId,
        _slot = self._slot,
        _lv = self._lv,
        _nextSkillId = self._nextSkillId,
        _isUnlock = self._isUnlock
    }
    return t
end

return PartnerSkill